package com.testin.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.thoughtworks.xstream.*;
import com.testin.model.Information;
import com.testin.model.ModelPage;
import com.testin.service.ReadModelService;
import com.testin.service.impl.ReadModelServiceImpl;

public class ReadModelServlet extends BaseServlet {
	private static final long serialVersionUID = 7422910046866929397L;
	
	private ReadModelService readModelService = new ReadModelServiceImpl();

	/**
	 * 获取模板数据
	 */
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		    List<ModelPage> modelPageList = readModelService.getModelPageList();	
	        XStream xs = new XStream();
    	    request.setCharacterEncoding("utf-8");
            response.setContentType("text/xml;charset=utf-8");         
            Information<Map<String, List<String>>> info =  new Information<Map<String, List<String>>>();                 
            Map<String, List<String>> temp = new HashMap<String, List<String>>();         
            for(ModelPage modelPage : modelPageList){	   	 
         	if (!temp.containsKey(modelPage.getKey())) {
    		temp.put(modelPage.getKey(),  new ArrayList<String>());
    	    }
    	    temp.get(modelPage.getKey()).add(modelPage.getValue());		
	        }
	        info.setData(temp);
            response.getWriter().print(xs.toXML(info));
		 } 	
	}
	

